package com.bjtu.boatmq.common.protocol;

import com.bjtu.boatmq.common.utils.BytesUtil;

/**
 * message类
 */
public class Message {
    private String topic;       // 主题
    private int partitionId;    // 分区ID
    private int offset;         // 偏移量
    private Object body;
    // private byte[] body;
    private String bodyClassName; // body类名

    public Message(){}
    public Message(String topic, int partitionId, Object body){
        this.topic = topic;
        this.partitionId = partitionId;
        this.body = body;
    }
    public Message(String topic, int partitionId, Object body,String bodyClassName) {
        this.topic = topic;
        this.partitionId = partitionId;
        this.body = body;
        this.bodyClassName = bodyClassName;
    }

    /**
     * 将Message对象转为Byte[]
     * @return
     */
    public byte[] getTypes(){
        byte [] bodyBytes = null;
        try{
            bodyBytes = BytesUtil.objectToBytes(this);
        }catch (Exception e){
            e.printStackTrace();
        }
        return bodyBytes;
    }

    /* getter and setter方法 */
    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public int getPartitionId() {
        return partitionId;
    }

    public void setPartitionId(int partitionId) {
        this.partitionId = partitionId;
    }

    public Object getBody() {
        return body;
    }

    public void setBody(Object body) {
        this.body = body;
    }

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public String getBodyClassName() {
        return bodyClassName;
    }

    public void setBodyClassName(String bodyClassName) {
        this.bodyClassName = bodyClassName;
    }

    @Override
    public String toString() {
        return "Message{" +
                "topic='" + topic + '\'' +
                ", partitionId=" + partitionId +
                ", body=" + body +
                ", bodyClassName='" + bodyClassName + '\'' +
                '}';
    }

}
